//
//  ShowView.m
//  CarHome
//
//  Created by 王建男 on 14-9-22.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "ShowView.h"
#import "CarModel_Tableview.h"
#import "CarModel_table.h"
#import "DetailedViewController.h"

NSString *const TableViewCellIdentifier_Model = @"Cell_FindCar_Model";

@implementation ShowView

- (void)dealloc
{
    [_tableSecArray release];
    _tableSecArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _tableSecArray = [[NSMutableArray alloc] init];
        [self creatView];
        [self creatTableView];
    }
    return self;
}

- (void)creatView
{
    _tureView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, self.frame.size.width - 80, self.frame.size.height)];
    [_tureView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:_tureView];
    [_tureView release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.frame.size.width - _tureView.frame.size.width, self.frame.size.height)];
    [button setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.7]];
    [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tureView.frame.size.width, 64)];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setText:@"选择车型"];
    [_tureView addSubview:lable];
    [lable release];

}

- (void)clickAction:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark creatTableView

- (void)creatTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, _tureView.frame.size.width, _tureView.frame.size.height - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewCellIdentifier_Model];
    
    [_tureView addSubview:_tableView];
    [_tableView release];
}

#pragma mark -
#pragma mark tableView协议
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CarModel_table *cartable = [_tableSecArray objectAtIndex:section];
    return [cartable.specitems count];
}

- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tableSecArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CarModel_table *carTable = [_tableSecArray objectAtIndex:section];
    return carTable.groupname;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier_Model forIndexPath:indexPath];
    
    CarModel_table *tableCar = [_tableSecArray objectAtIndex:indexPath.section];
    CarModel_Tableview *carTableview = [tableCar.specitems objectAtIndex:indexPath.row];
    
    [cell.textLabel setText:carTableview.name];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    CarModel_table *carTable = [_tableSecArray objectAtIndex:indexPath.section];
    CarModel_Tableview *carTableView = [carTable.specitems objectAtIndex:indexPath.row];
    self.block(carTableView.id, carTableView.name);
    [self removeFromSuperview];
}

- (void)setName:(NSMutableArray *)array AndSpeciteId:(NSString *)speciteId
{
    [_tableSecArray addObjectsFromArray:array];
    _speciteId = [NSString stringWithFormat:@"%@", speciteId];
}

#pragma mark -
#pragma mark block回调

- (void)showBlock:(ShowBlock)block
{
    self.block = block;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
